<?php
namespace app\mall\service;
/**
 * 商品服务
 */
class InfoService {

    public function getInfo($id) {
        return target('mall/mall')->getInfo($id);
    }

    public function getData($mallId, $proId, $info) {
        $proList = target('mall/mallProducts')->loadList(['mall_id' => $mallId], 0);
        $proInfo = [];
        if(!empty($proId)) {
            foreach($proList as $vo) {
                if($vo['products_id'] == $proId) {
                    $proInfo = $vo;
                }
            }
        }else {
            $proInfo = $proList[0];
        }
        if($proInfo) {
            $proInfo['spec_data'] = unserialize($proInfo['spec_data']);
        }
        $skuList = [];
        if(!empty($proList)){
            foreach($proList as $key => $vo) {
                $specData = unserialize($vo['spec_data']);
                $k = [];
                if(!empty($specData)) {
                    foreach($specData as $v) {
                        $k[] = $v['id'] . ':' . $v['value'];
                    }
                }
                $k = implode(',', $k);
                $skuList[$k] = $vo;
                $skuList[$k]['spec_data'] = unserialize($vo['spec_data']);
                if($vo['products_id'] == $proList[0]['products_id']) {
                    $skuList[$k]['url'] = url('index', ['id' => $mallId, 'pro_id' => 0]);
                }else {
                    $skuList[$k]['url'] = url('index', ['id' => $mallId, 'pro_id' => $vo['products_id']]);
                }
            }
        }
        $specData = [];
        if(!empty($proInfo['spec_data'])) {
            foreach($proInfo['spec_data'] as $vo) {
                $specData[$vo['id']] = $vo;
            }
            $proInfo['spec_data'] = $specData;
        }
        $specList = unserialize($info['spec_data']);
        if(!empty($specList)) {
            foreach($specList as $key => $vo) {
                $spec = explode(',', $vo['value']);
                $specList[$key]['value'] = $spec;
                if(in_array($specData[$key]['value'], $spec)) {
                    $specList[$key]['cur'] = $specData[$key]['value'];
                }
            }
        }

        return [
            'proInfo' => $proInfo ? $proInfo : [],
            'skuList' => $skuList ? $skuList : [],
            'specList' => $specList ? $specList : []
        ];
    }
}

